﻿
$(onPageLoad);

function onPageLoad() {

  // register event handlers on page
  $("#cmdUpdateSiteTitle").click(onUpdateSiteTitle);
  $("#cmdAddNewList").click(onAddNewList);
  $("#cmdUseCustomMaster").click(onUseCustomMaster);
  $("#cmduseAppMaster").click(onUseAppMaster);
  $("#results").append($("<img>", { src: "_layouts/images/gears_anv4.gif" }));

  // ensure sp.js is loaded and intialize CSOM objects
  SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { sharePointReady(); });
}

var ctx;
var web;
var lists;

function sharePointReady() {
  ctx = SP.ClientContext.get_current();
  web = ctx.get_web();
  ctx.load(web);
  ctx.load(web.get_currentUser());
  lists = web.get_lists();
  ctx.load(lists);
  ctx.executeQueryAsync(onDisplaySiteInfo, onError);
}

function onDisplaySiteInfo() {

  // get site info
  var siteTitle = web.get_title();
  var siteId = web.get_id().toString();
  var siteUrl = web.get_url();
  var siteMasterPage = web.get_masterUrl();
  var currentUser = web.get_currentUser().get_loginName();

  $("#results").empty();
  $("#results").append($("<h2>").html("Site properties"));

  var table = $("<table>");
  table.append($("<tr>").append($("<td>").html("Title:")).append($("<td>").html(siteTitle)));
  table.append($("<tr>").append($("<td>").html("ID:")).append($("<td>").html(siteId)));
  table.append($("<tr>").append($("<td>").html("Url:")).append($("<td>").html(siteUrl)));
  table.append($("<tr>").append($("<td>").html("Master Page:")).append($("<td>").html(siteMasterPage)));
  table.append($("<tr>").append($("<td>").html("User:")).append($("<td>").html(currentUser)));
  $("#results").append(table);


  $("#results").append($("<h2>").html("Lists"));

  var ol = $("<ol>");

  for (var i = 0; i < lists.get_count() ; i++) {
    var list = lists.get_item(i);
    var list_url = "xyz"; //list.get_defaultViewUrl();
    var list_title = list.get_title();
    var list_anchor = $("<a>", { href: list_url, alt: list_title }).text(list_title);
    ol.append($("<li>").append(list_anchor));
  }

  $("#results").append(ol);

  $("span.ms-core-siteTitle span a").html(web.get_title());

}

function onError(sender, args) {
  $get("results").innerHTML = "Error: " + JSON.stringify(args);
}

function onUpdateSiteTitle() {

  var dialogArgs = {
    title: web.get_title()
  };

  var dialogOptions = {
    url: "UpdateSiteTitle.aspx",
    title: "Update Site Title",
    width: "480px",
    args: dialogArgs,
    dialogReturnValueCallback: UpdateSiteTitleCallback

  };

  SP.UI.ModalDialog.showModalDialog(dialogOptions);
}

function UpdateSiteTitleCallback(dialogResult, returnValue) {

  if (dialogResult == SP.UI.DialogResult.OK) {
    web.set_title(returnValue.title);
    web.update();
    ctx.executeQueryAsync(onDisplaySiteInfo, onError);
  }

}


function onAddNewList() {

  var dialogArgs = {
    title: web.get_title()
  };

  var dialogOptions = {
    url: "AddList.aspx",
    dialogReturnValueCallback: AddListCallback,
    args: dialogArgs,
    title: "Add List"
  };

  SP.UI.ModalDialog.showModalDialog(dialogOptions);
}

function AddListCallback(dialogResult, returnValue) {

  if (dialogResult == SP.UI.DialogResult.OK) {

    $("#results").empty();
    $("#results").append($("<img>", { src: "_layouts/images/gears_anv4.gif" }));


    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(returnValue.title);
    listCreationInfo.set_quickLaunchOption(SP.QuickLaunchOptions.on);

    var listType;
    switch (returnValue.listType) {
      case "Custom":
        listType = SP.ListTemplateType.genericList;
        break;
      case "Announcements":
        listType = SP.ListTemplateType.announcements;
        break;
      case "Contacts":
        listType = SP.ListTemplateType.contacts;
        break;
      case "Tasks":
        listType = SP.ListTemplateType.tasks;
        break;
      case "Documents":
        listType = SP.ListTemplateType.documentLibrary;
        break;
      default:
        listType = SP.ListTemplateType.genericList;
        break;
    }
    listCreationInfo.set_templateType(listType);

    lists.add(listCreationInfo);
    ctx.load(lists);
    ctx.executeQueryAsync(onDisplaySiteInfo, onError);
  }

}

function onUseCustomMaster() {
  var masterUrl = web.get_serverRelativeUrl() + "/_catalogs/masterpage/CustomApp.master";
  web.set_masterUrl(masterUrl);
  web.update();
  ctx.executeQueryAsync(refreshPage, onError);
}

function onUseAppMaster() {
  var masterUrl = web.get_serverRelativeUrl() + "/_catalogs/masterpage/app.master";
  web.set_masterUrl(masterUrl);
  web.update();
  ctx.executeQueryAsync(refreshPage, onError);
}

function refreshPage() {
  location.reload();
}